﻿using System;
using System.Data;
using DataAccess;

namespace Business
{
    public class PostBll
    {
        private PostDao _postDao;

        /// <summary>
        /// Initializes a new instance of the <see cref="PostBll"/> class.
        /// </summary>
        public PostBll()
        {
            _postDao = new PostDao();
        }

        /// <summary>
        /// Views all posts.
        /// </summary>
        /// <returns></returns>
        public DataTable ViewAllPosts()
        {
            try
            {
                return _postDao.ViewAllPost();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Reads the post.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public DataTable ReadPost(int id)
        {
            try
            {
                _postDao.UpdatePostViews(id);
                return _postDao.ReadAPost(id);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Views the post by user id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public DataTable ViewPostByUserId(int id)
        {
            try
            {
                return _postDao.ViewPostByUserId(id);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Searches the post.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <returns></returns>
        public DataTable SearchPost(string title)
        {
            try
            {
                return _postDao.SearchPost(title);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Deletes the post.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public bool DeletePost(int id)
        {
            try
            {
                int result = _postDao.DeletePost(id);
                return (result != 0);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Deletes the post by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public bool DeletePostByUserId(int userId)
        {
            try
            {
                int result = _postDao.DeletePostByUserId(userId);
                return (result != 0);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// Creates the post.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="shortDesc">The short desc.</param>
        /// <param name="shortDescription"> </param>
        /// <param name="content">The content.</param>
        /// <param name="image">The image.</param>
        /// <param name="date">The date.</param>
        /// <param name="status">The status.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="category">The category.</param>
        /// <param name="categoryId"> </param>
        /// <returns></returns>
        public bool CreatePost(string title, string shortDescription, string content, 
            string image, DateTime date, int status, int userId, int categoryId)
        {
            try
            {
                int result = _postDao.CreatePost(title, shortDescription, content, image, 
                    date, status, userId, categoryId);
                return (result != 0);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Edits the post.
        /// </summary>
        /// <param name="postId">The post id.</param>
        /// <param name="title">The title.</param>
        /// <param name="shortDescription">The short description.</param>
        /// <param name="content">The content.</param>
        /// <param name="imageLink">The image link.</param>
        /// <param name="datePost">The date post.</param>
        /// <param name="status">The status.</param>
        /// <param name="cateId">The cate id.</param>
        /// <returns></returns>
        public bool EditPost(int postId, string title, string shortDescription, string content,
            string imageLink, DateTime datePost, int status, int categoryId)
        {
            try
            {
                int result = _postDao.EditPost(postId, title, shortDescription, content, imageLink, datePost, status,
                                               categoryId);
                return (result != 0);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }

}
